﻿#region License
// (c) Garry McGlennon 2009
// 
// This project is made available under the Microsoft Public License (Ms-PL)
// Code is provided as is and with no warrenty – Use at your own risk
// 
// View the project and the latest code at http://dnpframework.codeplex.com/
// 
// All other rights reserved.
// 
// blog: www.dotNetProfessional.com/blog/
#endregion
using dnp.Framework.Database.DatabaseExplorer;
using MbUnit.Framework;

namespace dnp.Framework.UnitTest.Database.DatabaseExplorer
{
    [TestFixture]
    public class RoutineParameterTest
    {
        private Framework.Database.DatabaseExplorer.Database _database;

        [TestFixtureSetUp]
        public void Setup()
        {
            _database = new Framework.Database.DatabaseExplorer.Database("ReferenceSchema", null);
        }

        #region Function Test
        [Test]
        public void Function_Name()
        {
            Routine r = _database.Routines["dbo.fn_RetrieveCarWithParameter"];

            Assert.AreEqual("@CarId", r.Parameters[0].Name);

        }
        [Test]
        public void Function_Alias()
        {
            Routine r = _database.Routines["dbo.fn_RetrieveCarWithParameter"];

            Assert.AreEqual("@CarId", r.Parameters[0].Alias);

        }
        [Test]
        public void Function_ParameterCount()
        {
            Routine r = _database.Routines["dbo.fn_RetrieveCarWithParameter"];

            Assert.AreEqual(1, r.Parameters.Count);
            
        }
        [Test]
        public void Function_Description()
        {
            // Check that descriptions are correct. (not supported for Views so just empty string)
            Routine r = _database.Routines["dbo.fn_RetrieveCarWithParameter"];

            // Check that aliased fields have thier alias
            Assert.AreEqual("", r.Columns["CarId"].Description);
        }
        [Test]
        public void Function_DataType()
        {
            Routine r = _database.Routines["dbo.fn_RetrieveCarWithParameter"];
            Assert.AreEqual("int", r.Parameters[0].DataType);
        }
        [Test]
        public void Function_DotNetType()
        {
            Routine r = _database.Routines["dbo.fn_RetrieveCarWithParameter"];
            Assert.AreEqual(typeof(int), r.Parameters[0].DotNetType);
        }

        [Test]
        public void Function_OrdinalPosition()
        {
            Routine r = _database.Routines["dbo.fn_RetrieveCarWithParameter"];
            RoutineParameterColumn c;

            c = r.Parameters["@CarId"];
            Assert.AreEqual(1, c.OrdinalPosition);
        }
        [Test]
        public void Function_Parent()
        {
            Routine r = _database.Routines["dbo.fn_RetrieveCarWithParameter"];
            RoutineParameterColumn c;

            c = r.Parameters["@CarId"];
            Assert.AreEqual(r.Name, c.Parent.Name);
        }

        #endregion

        #region Stored Procedures

        [Test]
        public void StoredProcedure_Name()
        {
            Routine r = _database.Routines["dbo.usp_CarSearch"];

            // Test First
            Assert.AreEqual("@whereClause", r.Parameters[0].Name);
            // Test Last
            Assert.AreEqual("@Param10", r.Parameters[r.Parameters.Count - 1].Name);

        }
        [Test]
        public void StoredProcedure_Alias()
        {
            Routine r = _database.Routines["dbo.usp_CarSearch"];

            Assert.AreEqual("@whereClause", r.Parameters[0].Alias);

        }
        [Test]
        public void StoredProcedure_ParameterCount()
        {
            Routine r = _database.Routines["dbo.usp_CarSearch"];

            Assert.AreEqual(14, r.Parameters.Count);
        }
        [Test]
        public void StoredProcedure_Description()
        {
            Routine r = _database.Routines["dbo.usp_CarSearch"];
            // Test First
            Assert.AreEqual("", r.Parameters[0].Description);
            // Test Last
            Assert.AreEqual("", r.Parameters[r.Parameters.Count - 1].Description);

        }

        [Test]
        public void StoredProcedure_DataType()
        {
            Routine r = _database.Routines["dbo.usp_CarSearch"];
            Assert.AreEqual("nvarchar", r.Parameters[0].DataType);
        }
        [Test]
        public void StoredProcedure_DotNetType()
        {
            Routine r = _database.Routines["dbo.usp_CarSearch"];
            Assert.AreEqual(typeof(string), r.Parameters[0].DotNetType);
        }

        [Test]
        public void StoredProcedure_OrdinalPosition()
        {
            Routine r = _database.Routines["dbo.usp_CarSearch"];
            RoutineParameterColumn c;

            c = r.Parameters["@whereClause"];
            Assert.AreEqual(1, c.OrdinalPosition);
        }
        [Test]
        public void StoredProcedure_Parent()
        {
            Routine r = _database.Routines["dbo.usp_CarSearch"];
            RoutineParameterColumn c;

            c = r.Parameters["@whereClause"];
            Assert.AreEqual(r.Name, c.Parent.Name);
        }
        #endregion
    }
}
